<?php

namespace App\Controllers\Admin;

use Page,Rate,View,Area;
use Input, Notification, Redirect, Sentry, Str;

use App\Services\Validators\PageValidator;

class AreasController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        $user = Sentry::getUser();


        $METHOD = 'areas';
        $ACTION = 'index';
        $areas = Area::where('father_id',$user->area)->where('city_id',$user->city)->orderBy('created_at', 'DESC')->paginate(20);
//        $services = Service::where('section_id',$user->area)->orderBy('created_at', 'DESC')->paginate(20);
//        $works = Work::where('section_id',$user->area)->orderBy('created_at', 'DESC')->get();
        return View::make('admin.areas.index',compact('METHOD','ACTION','areas'));

	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{


	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
        $area = Area::find(Input::get('area_id'));
        $area->data1 = Input::get('data1');
        $area->data2 = Input::get('data2');
        $area->save();
        return Redirect::route('admin.areas.index');
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{

	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//


        require app_path().'/lib/qiniu/rs.php';
        require app_path().'/lib/qiniu/io.php';

        $area = Area::find($id);
        if($_FILES['cover']["error"] == UPLOAD_ERR_OK) {
            $area->cover = $this->uploadfile($_FILES["cover"]["name"], $_FILES["cover"]["tmp_name"]);
        }
        $area->x = Input::get('D1pFYkAA');
        $area->y = Input::get('D1pFYl0A');
        $area->save();
        return Redirect::route('admin.areas.index');
	}


    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function service_update($id)
    {
        //


        $area = Area::find($id);
        $area->service_x = Input::get('D1pFYkAA');
        $area->service_y = Input::get('D1pFYl0A');
        $area->service_address = Input::get('service_address');
        $area->save();
        return Redirect::route('admin.areas.index');
    }


    public function uploadfile($name,$tmp){
        $type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型

        if(!in_array(strtolower($this->fileext($name)),$type))
        {
            $text=implode(",",$type);
            echo "您只能上传以下类型文件: ",$text,"<br>";
        }
        //生成目标文件的文件名
        else{
            $filename=explode(".",$name);
            $filename[0]=$this->random(10); //设置随机数长度
            $uploadfile=implode(".",$filename);
            //$name1=$name.".Mcncc";

            $result = $this->upPicTo7niu($tmp,'com',$uploadfile,'aebo');
            return $result['key'];
        }
    }


    /**
     *上传图片至七牛云
     *$path 图片地址
     *$dirname 在七牛云上的文类名称
     *$filename 文件保存名称
     *$bucket 你的七牛云域名称
     */
    function upPicTo7niu($path,$dirname,$filename,$bucket){
        $contents = @file_get_contents($path);

        $storename = $dirname."/".$filename;
        $bu = $bucket.":".$storename;
        $accessKey = 'FWdy0abUDmanFns5ZArKEHjRl2e5hXeZVsLKSU7M';
        $secretKey = 'zuHKtuuL888TuuavvzbyWnGnytjW33zI1mrAql12';

        Qiniu_SetKeys($accessKey, $secretKey);
        $putPolicy = new \Qiniu_RS_PutPolicy($bu);
        $upToken = $putPolicy->Token(null);
        $putExtra = new \Qiniu_PutExtra();
        $putExtra->Crc32 = 1;
        list($ret, $err) = Qiniu_Put($upToken, $storename, $contents, $putExtra);
        return $ret;
    }


    //获取文件后缀名函数
    function fileext($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }

    function random($length)
    {
        $hash = 'Area-';
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($chars) - 1;
        mt_srand((double)microtime() * 1000000);
        for($i = 0; $i < $length; $i++)
        {
            $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;
    }

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
        $rate = Rate::findOrFail($id);
        $rate->delete();
//        Flash::success(lang('Operation succeeded.'));

        return Redirect::route('rates.index');

	}

    public function lbs(){
        $id = Input::get('id');
        $user = Sentry::getUser();

        $area = Area::find($id);

        if($area->x == ''){
            $data['set'] = 0;
        }else{
            $data['x'] = $area->x;
            $data['y'] = $area->y;
            $data['set'] = 1;
        }


        $METHOD = 'areas';
        $ACTION = 'index';
        return View::make('admin.areas.lbs',compact('METHOD','ACTION','area','data'));
    }



    public function service_lbs(){
        $id = Input::get('id');
        $user = Sentry::getUser();

        $area = Area::find($id);

        if($area->service_x == ''){
            $data['set'] = 0;
        }else{
            $data['service_x'] = $area->x;
            $data['service_y'] = $area->y;
            $data['set'] = 1;
        }


        $METHOD = 'areas';
        $ACTION = 'index';
        return View::make('admin.areas.service_lbs',compact('METHOD','ACTION','area','data'));
    }




    public function delete(){
        $id = Input::get('id');
        Area::destroy($id);

//        Flash::success(lang('Operation succeeded.'));

        return Redirect::back();
    }
    public function wangdian(){

        $METHOD = 'wangdian';
        $ACTION = 'index';

        $sort_city = Input::get('city');
        if(!$sort_city){
            $sort_city = 'undefined';
        }
        $sort_section = Input::get('section');
        if(!$sort_section){
            $sort_section = 'undefined';
        }

        $user = Sentry::getUser();

        $cities = Area::where('level',2)->where('name','!=','')->get();
        $sections = '';

        if($user->hasAccess('admin')){
            if($sort_section != 'all'){
                $areas = Area::where('father_id',$sort_section)->where('service_address','!=','')->where('service_address','!=','0')->orderBy('created_at', 'DESC')->paginate(20);

            }elseif($sort_city == 'all'){
                $areas = Area::where('service_address','!=','')->where('service_address','!=','0')->orderBy('created_at', 'DESC')->paginate(20);
            }else{
                $areas = Area::where('city_id',$sort_city)->where('service_address','!=','')->where('service_address','!=','0')->orderBy('created_at', 'DESC')->paginate(20);

            }

        }elseif($user->hasAccess('city')){

            if($sort_section == all || $sort_section == 'undefined'){
                $areas = Area::where('city_id',$user->city)->where('service_address','!=','')->where('service_address','!=','0')->orderBy('created_at', 'DESC')->paginate(20);


            }else{
                $areas = Area::where('father_id',$sort_section)->where('city_id',$user->city)->where('service_address','!=','')->where('service_address','!=','0')->orderBy('created_at', 'DESC')->paginate(20);


            }
            $sections = Area::where('level',3)->where('father_id',$user->city)->where('name','!=','')->get();

        }elseif($user->hasAccess('section')){
            $areas = Area::where('father_id',$user->area)->where('city_id',$user->city)->where('service_address','!=','')->where('service_address','!=','0')->orderBy('created_at', 'DESC')->paginate(20);

        }



        //


//        $services = Service::where('section_id',$user->area)->orderBy('created_at', 'DESC')->paginate(20);
//        $works = Work::where('section_id',$user->area)->orderBy('created_at', 'DESC')->get();
        return View::make('admin.areas.wangdian',compact('METHOD','ACTION','areas','cities','sections','user','sort_city','sort_section'));

    }

    public function get_section(){

//        var_dump(Input::all());
        $id = Input::get('id');
        $areas = Area::where('father_id',$id)->get();

        return View::make('admin.areas.get_section',compact('areas'));
    }



}
